#! /usr/bin/python
#coding=utf-8

from sqlalchemy import *
from sqlalchemy.orm import mapper, sessionmaker
import urllib,re
import codecs #处理"°C" 这样的符号用
look  =  codecs.lookup( " utf-8 " )

class Category(object):
    pass

class Product(object):
    pass

class ProductExt(object):
    pass

class Cat:
    def __init__(self):
        self.engine =create_engine('mysql://root:123456@localhost/focal?charset=utf8',echo=True)
        #self.engine =create_engine('mysql://focal:focalweiming@localhost/focal?charset=utf8',echo=True)
        metadata = MetaData()
        category_table=Table('category',metadata,
            Column('id',Integer, primary_key=True),
            Column('name',CHAR(255)),
            Column('url',CHAR(255)),
            Column('title',CHAR(255)),
            Column('keywords',CHAR(255)),
            Column('description',Text),
            Column('desc',Text),
            Column('url_code',Unicode(255)),
            Column('level',Unicode(255)),
            Column('paths',CHAR(255)),
            Column('lang',Unicode(255)),
        )
        product_table=Table('product',metadata,
            Column('id',Integer, primary_key=True),
            Column('name',CHAR(255)),
            Column('url_code',Unicode(255), primary_key=True),
            Column('url',Unicode(255)),
            Column('cat_code',Unicode(255)),
            Column('image',Unicode(255)),
            Column('price',Unicode(255)),
            Column('page',Integer),
            Column('lang',Unicode(255)),
        )
        productext_table=Table('product_ext',metadata,
            Column('id',Integer, primary_key=True),
            Column('product_id',Integer),
            Column('images',Text),
            Column('summary',Text),
            Column('description_m',Text),
            Column('seo_description',Text),
            Column('seo_title',Text),
            Column('seo_keywords',Text),
            Column('lang',Unicode(255)),
        )
        metadata.create_all(self.engine)
        mapper(Category,category_table)
        mapper(Product,product_table)
        mapper(ProductExt,productext_table)
        Session = sessionmaker()
        Session.configure(bind=self.engine)
        self.db = Session()

    def test(self):
        v1='Lenovo A850％SmartPhone MT6582m Quad Core IPS 5.5 Inch Android 4.2 1GB 4GB WCDMA 3G Multiple Languages％'
        v2=urllib.quote(v1)
        v3=look.decode(v2)
        v4=self.strQ2B(v1)
        print v2
        print v3
        print v4
        exit()
        pm = Product()
        pm.name=v1
        pm.url='www.iic.cn'
        pm.cat_code='1'
        pm.image='dsdsada'
        pm.price='12'
        pm.page='1'
        pm.lang='1'
        self.db.add(pm)
        self.db.flush()
        self.db.commit()

    def strQ2B(self,ustring):
        ustring=ustring.decode("utf-8",'ignore')
        rstring=""
        for uchar in ustring:
            inside_code=ord(uchar)
            print inside_code
            if inside_code==0x3000:
                inside_code=0x0020
            else:
                inside_code-=0xfee0
            if inside_code<0x0020 or inside_code>0x7e:
                rstring+=uchar.encode('utf-8','ignore')
            else:
                rstring+=(unichr(inside_code)).encode('utf-8','ignore')
        return rstring

if __name__ == '__main__':
    print "Prepare data. is come soon ..."
    cat=Cat()
    cat.test()
    print "OK!!!"

